package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Service
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;

	@Autowired
	private UserAccountDetailMapper userAccountDetailMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveUserAccount(Long userId) {
		//1.创建账户对象，新增账户
		UserAccount userAccount = new UserAccount();
		userAccount.setUserId(userId);
		userAccount.setTotalAmount(new BigDecimal("100"));
		userAccount.setAvailableAmount(new BigDecimal("100"));
		userAccount.setTotalIncomeAmount(new BigDecimal("100"));
		userAccountMapper.insert(userAccount);
		//2.新增账户变动历史（首次注册赠送100点）
		if (userAccount.getAvailableAmount().intValue() > 0) {
			this.saveUserAccountDetail(userId, "赠送", SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT, userAccount.getAvailableAmount(), null);
		}
	}

	/**
	 * 新增账户变动日志
	 * @param userId 用户ID
	 * @param title 资金变动缘由
	 * @param tradeType 交易类型
	 * @param amount 变动金额
	 * @param orderNo 订单编号
	 */
	@Override
	public void saveUserAccountDetail(Long userId, String title, String tradeType, BigDecimal amount, String orderNo) {
		UserAccountDetail userAccountDetail = new UserAccountDetail();
		userAccountDetail.setUserId(userId);
		userAccountDetail.setTitle(title);
		userAccountDetail.setTradeType(tradeType);
		userAccountDetail.setAmount(amount);
		userAccountDetail.setOrderNo(orderNo);
		userAccountDetailMapper.insert(userAccountDetail);
	}

	/**
	 * 获取账户可用余额
	 * @return
	 */
	@Override
	public BigDecimal getAvailableAmount() {
		Long userId = AuthContextHolder.getUserId();
		LambdaQueryWrapper<UserAccount> userAccountLambdaQueryWrapper = new LambdaQueryWrapper<>();
		userAccountLambdaQueryWrapper.eq(UserAccount::getUserId,userId);
		UserAccount userAccount = userAccountMapper.selectOne(userAccountLambdaQueryWrapper);
		if(userAccount != null) return userAccount.getAvailableAmount();
		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void checkAndLock(AccountLockVo accountLockVo) {
		// 扣减金额
		int count = userAccountMapper.checkAndLock(accountLockVo);
		// 扣减明细
		if(count <= 0){
			throw new GuiguException(400,"账户余额不足!");
		}

		this.saveUserAccountDetail(
				accountLockVo.getUserId(),
				accountLockVo.getContent(),
				SystemConstant.ACCOUNT_TRADE_TYPE_MINUS,
				accountLockVo.getAmount(),
				accountLockVo.getOrderNo());
	}
}















